﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate.Linq;

namespace GLAD_prototype
{
    public partial class frmAlbum : Form
    {
        public frmAlbum()
        {
            InitializeComponent();
        }

        private void frmAlbum_Load(object sender, EventArgs e)
        {
            CRUD.updateDGV<album>(dgvAlbum);
            CRUD.updateDGV<lagu>(dgvLagu);
        }

        private void btnInsertAlbum_Click(object sender, EventArgs e)
        {
            album Album;
            CRUD.Insert<album>(Album = new album
                                {
                                    albumID = txtAlbumID.Text,
                                    judulAlbum = txtJudulAlbum.Text,
                                    genre = cbGenre.Text,
                                    label = txtLabel.Text,
                                    tglRilis = Convert.ToDateTime(dtpTglRilis.Text),
                                    tipeKeping = cbTipeKeping.Text,
                                    descAlbum = txtDeskripsiAlbum.Text,
                                    userCount = 0,
                                    userRating = 0
                                }
                              );
            CRUD.updateDGV<album>(dgvAlbum);
        }

        private void btnDeleteAlbum_Click(object sender, EventArgs e)
        {
            CRUD.Delete<album>(m => m.albumID == txtAlbumID.Text);
            CRUD.updateDGV<album>(dgvAlbum);
        }

        private void txtUpdateAlbum_Click(object sender, EventArgs e)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                album theAlbum = session.Query<album>()
                                   .Where(m => m.albumID == txtAlbumID.Text)
                                   .Select(m => m).Single();

                theAlbum.genre = cbGenre.Text;
                theAlbum.judulAlbum = txtJudulAlbum.Text;
                theAlbum.label = txtLabel.Text;
                theAlbum.tglRilis = Convert.ToDateTime(dtpTglRilis.Text);
                theAlbum.tipeKeping = cbTipeKeping.Text;
                theAlbum.descAlbum = txtDeskripsiAlbum.Text;

                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(theAlbum);
                    transaction.Commit();
                }
            }
        }

        private void btnInsertLagu_Click(object sender, EventArgs e)
        {
            lagu Lagu;
            CRUD.Insert<lagu>(Lagu = new lagu
                                {
                                    songID = txtSongID.Text,
                                    judulLagu = txtJudulLagu.Text,
                                    durasi = TimeSpan.Parse(txtDurasi.Text),
                                    descLagu = txtDeskripsiLagu.Text
                                }
                             );

            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    lagu theSong = session.Query<lagu>()
                                   .Where(m => m.songID == txtSongID.Text)
                                   .Select(m => m).Single();
                    artist theArtist = session.Query<artist>()
                                       .Where(m => m.artistID == txtArtis.Text)
                                       .Select(m => m).Single();
                    album theAlbum = session.Query<album>()
                                     .Where(m => m.albumID == txtAlbumID2.Text)
                                     .Select(m => m).Single();
                    theSong.Artist = theArtist;
                    theSong.Album = theAlbum;

                    session.SaveOrUpdate(theSong);
                    transaction.Commit();
                }
            }

            CRUD.updateDGV<lagu>(dgvLagu);
            
        }

        private void btnUpdateLagu_Click(object sender, EventArgs e)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                lagu theSong = session.Query<lagu>()
                                   .Where(m => m.songID == txtSongID.Text)
                                   .Select(m => m).Single();
                artist theArtist = session.Query<artist>()
                                       .Where(m => m.artistID == txtArtis.Text)
                                       .Select(m => m).Single();
                album theAlbum = session.Query<album>()
                                 .Where(m => m.albumID == txtAlbumID2.Text)
                                 .Select(m => m).Single();

                theSong.Artist = theArtist;
                theSong.Album = theAlbum;
                theSong.judulLagu = txtJudulLagu.Text;
                theSong.durasi = TimeSpan.Parse(txtDurasi.Text);
                theSong.descLagu = txtDeskripsiLagu.Text;

                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(theSong);
                    transaction.Commit();
                }
            }
        }

        private void btnDeleteLagu_Click(object sender, EventArgs e)
        {
            CRUD.Delete<lagu>(m => m.songID == txtSongID.Text);
            CRUD.updateDGV<lagu>(dgvLagu);
        }
    }
}
